import Router from 'koa-router';
import axios from './utils/axios'
import dbsConfig from '../dbs/config'
import Pois from '../dbs/models/pois'
import {
  product
} from './utils/product'
// console.log(product);
const {
  sign,
  requestUrl
} = dbsConfig
const router = new Router({
  prefix: '/search'
})


//  /search/top 检索当地热门
router.get('/top', async (ctx) => {
  // let top = await Pois.find({
  //   name: new RegExp(ctx.query.input)
  // })
  // ctx.body = top.length ? top.map((item) => {
  //   return {
  //     name: item.name,
  //     type: item.type
  //   }
  // }) : []
  let {
    status,
    data: {
      top
    }
  } = await axios.get(`http://cp-tools.cn/search/top`, {
    params: {
      input: ctx.query.input,
      city: ctx.query.city,
      sign
    }
  })
  ctx.body = top.length ? top.map((item) => {
    return {
      name: item.name,
      type: item.type
    }
  }) : []
})


//  热门搜索
router.get('/hotPlace', async (ctx) => {
  // let top = await Pois.find({
  //   city: ctx.query.city || ctx.store.state.geo.city.replace('市',''),
  //   // 库里只有丽人
  //   type: ctx.query.type || '丽人'
  // })
  // ctx.body = top.length ? top.map((item) => {
  //   return {
  //     name: item.name,
  //     type: item.type
  //   }
  // }) : []
  const {
    status,
    data: {
      result
    }
  } = await axios.get(`${requestUrl}/search/hotPlace`, {
    params: {
      city: ctx.query.city || ctx.store.state.geo.city.replace("市", ''),
      sign
    }
  })
  ctx.body = status === 200 && result.length ? result.map((item) => {
    return {
      name: item.name,
      type: item.type
    }
  }) : []
})

// 关键词 artistic
router.get('/resultsByKeywords', async (ctx) => {
  // const data = await Pois.find({
  //   // name: new RegExp(ctx.query.keyword),
  //   city: '三亚'
  // })
  // ctx.body = {
  //   count: data.length,
  //   pois: data.map(item=> item.photos = [{url: item.img}])
  // }
  const {city, keyword} = ctx.query;
  let {
    status,
    data: {
      count,
      pois
    }
  } = await axios.get('http://cp-tools.cn/search/resultsByKeywords', {
    params: {
      city,
      keyword,
      sign
    }
  })
  ctx.body = {
    count: status === 200 ? count : 0,
    pois: status === 200
      ? pois
      : []
  }
})
// 产品详情
router.get('/product/:id', async (ctx) => {
  // const res = await Pois.find({
  //   _id: ctx.params.id
  // })
  // const {
  //   detailInfo,
  //   photos,
  //   dealList
  // } = product
  // ctx.body = {
  //   summary: res,
  //   // 登录状态判断 ctx.isAuthenticated()
  //   detailInfo,
  //   photos,
  //   // 商家优惠
  //   dealList: ctx.isAuthenticated() ? dealList : []
  // }
  
  let keyword = ctx.query.keyword || '旅游'
  let city = ctx.query.city || '北京'
  let {
    status,
    data: {
      product,
      more
    }
  } = await axios.get('http://cp-tools.cn/search/products', {
    params: {
      keyword,
      city,
      sign
    }
  })
  if (status === 200) {
    ctx.body = {
      product,
      more: ctx.isAuthenticated() ? more: [],
      login: ctx.isAuthenticated()
    }
  }else{
    ctx.body = {
      product: {},
      more: ctx.isAuthenticated() ? more: [],
      login: ctx.isAuthenticated()
    }
  }
})

export default router
